from flask_sqlalchemy import SQLAlchemy
from app import db
from flask_login import UserMixin
from datetime import datetime, timezone



class User(db.Model, UserMixin):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    nickname = db.Column(db.String(64), unique=True, default='unknown')
    avatar = db.Column(db.String(255), default='/static/images/avatars/default_avatar.png')
    gender = db.Column(db.String(10), default='unknown')
    address = db.Column(db.String(255), default='unknown')
    email = db.Column(db.String(120), unique=True)
    password = db.Column(db.String(255))
    createdTime = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
    updatedTime = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
    
    def __init__(self, name, email, password, nickname='unknown', avatar='/static/images/avatars/default_avatar.png', gender='unknown', address='unknown'):
        self.name = name
        self.email = email
        self.password = password
        self.nickname = nickname
        self.avatar = avatar
        self.gender = gender
        self.address = address
        
    # User与Lyrics的一对多关系
    lyrics = db.relationship('Lyrics', backref='author', lazy='dynamic')

class Lyrics(db.Model, UserMixin):
    """歌词模型"""
    __tablename__ = 'lyrics'
    
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100))
    content = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    
    # 与分镜的一对多关系
    storyboards = db.relationship('Storyboard', backref='lyrics', lazy='dynamic')
    
    def __init__(self, title, content, user_id):
        self.title = title
        self.content = content
        self.user_id = user_id

    def to_dict(self):
        """序列化为JSON数据"""
        return {
            'id': self.id,
            'title': self.title,
            'content': self.content,
            'user_id': self.user_id,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None,
            'storyboard_count': self.storyboards.count()
        }

class Storyboard(db.Model, UserMixin):
    """分镜模型"""
    __tablename__ = 'storyboards'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    lyrics_segment = db.Column(db.Text)  # 对应的歌词片段
    description = db.Column(db.Text)     # 文本描述
    image_path = db.Column(db.String(255))  # 生成图片的路径
    created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
    lyrics_id = db.Column(db.Integer, db.ForeignKey('lyrics.id'))
    style = db.Column(db.String(50))     # 风格设置
    sequence = db.Column(db.Integer, default=1) # 分镜序列号

    def __init__(self, name, lyrics_segment, description, image_path, lyrics_id, sequence, style="standard"):
        self.name = name
        self.lyrics_segment = lyrics_segment
        self.description = description
        self.image_path = image_path
        self.lyrics_id = lyrics_id
        self.style = style
        self.sequence = sequence

    def to_dict(self):
        """序列化为JSON数据"""
        return {
            'id': self.id,
            'name': self.name,
            'lyrics_segment': self.lyrics_segment,
            'description': self.description,
            'image_url': f"/static/{self.image_path}" if self.image_path else None,
            'style': self.style,
            'lyrics_id': self.lyrics_id
        }